#include <bits/stdc++.h>

using namespace std;
using ll = long long;
const int MAXN = 1e5 + 10;

int n, m, S[MAXN];
vector<int>a[MAXN];
void solve() {
    scanf("%d%d", &m, &n);
    for (int i = 1;i <= m;++i)a[i].resize(n + 2);
    for (int i = 1;i <= m;++i)for (int j = 1;j <= n;++j)scanf("%d", &a[i][j]);
    for (int i = 1;i <= n;++i) {
        S[i] = 0;
        for (int j = 1;j <= m;++j)S[i] = max(S[i], a[j][i]);
    }
    int ans = 0;
    for (int i = 1;i <= m;++i) {
        int t1 = 0, t2 = 0;
        for (int j = 1;j <= n;++j) {
            if (a[i][t1] < a[i][j])t2 = t1, t1 = j;
            else if (a[i][t2] < a[i][j])t2 = j;
        }
        int sum = min(a[i][t1], a[i][t2]);
        for (int j = 1;j <= n;++j)if (j != t1 && j != t2)sum = min(sum, S[j]);
        ans = max(ans, sum);
    }
    printf("%d\n", ans);
    for (int i = 1;i <= n;++i)a[i].resize(0);
}

int main() {
    int t;
    cin >> t;
    while(t--) solve();
    return 0;
}